package com.example.view;

import com.example.qzzm.R;
import com.example.window.WindowUtil;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ViewFlipper;

/**
 * 自定义的主界面的列表
 * 
 * @author Administrator
 *
 */
public class MyListView extends ListView {
	private String TAG = "MyListView";
	// 头部视图
	private LinearLayout mHeaderView;
	private ViewFlipper viewFlipper;
	private final Context context;

	public MyListView(Context context) {
		this(context, null);
	}

	public MyListView(Context context1, AttributeSet attrs) {
		super(context1, attrs);
		this.context = context1;
	}
	
	// 对外的接口
	// 是否显示Header
	public void addHeader() {
		mHeaderView = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.listview_header, null);
		viewFlipper = (ViewFlipper) mHeaderView.findViewById(R.id.vf_images);
		// 重新设置图片的宽度和高度
		WindowUtil wUtil = new WindowUtil(context);
		ImageView ivImage1 = (ImageView) mHeaderView
				.findViewById(R.id.iv_head_image1);
		ImageView ivImage2 = (ImageView) mHeaderView
				.findViewById(R.id.iv_head_image2);
		ImageView ivImage3 = (ImageView) mHeaderView
				.findViewById(R.id.iv_head_image3);
		android.view.ViewGroup.LayoutParams layoutParams = ivImage1
				.getLayoutParams();
		layoutParams.width = wUtil.getWidth();
		layoutParams.height = wUtil.px2dp(284);
		ivImage1.setLayoutParams(layoutParams);
		ivImage2.setLayoutParams(layoutParams);
		ivImage3.setLayoutParams(layoutParams);

		// 设置轮播图
		viewFlipper.setOnTouchListener(new OnTouchListener() {
			private float startX;

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					Log.v(TAG, "ACTION_DOWN");
					startX = event.getX();
					break;
				case MotionEvent.ACTION_UP:
					Log.v(TAG, "ACTION_UP");
					if (event.getX() > startX) { // 向右滑动
						viewFlipper
								.setInAnimation(context, R.anim.in_leftright);
						viewFlipper.setOutAnimation(context,
								R.anim.out_leftright);
						viewFlipper.showNext();
					} else if (event.getX() < startX) { // 向左滑动
						viewFlipper
								.setInAnimation(context, R.anim.in_rightleft);
						viewFlipper.setOutAnimation(context,
								R.anim.out_rightleft);
						viewFlipper.showPrevious();
					}
					break;
				}
				return true;
			}
		});
		addHeaderView(mHeaderView);
		// setAdapter(null);
	}

}
